import $router from './router'
import $global from './global.vue'
import $axios from 'axios'
import main from './main.js'

// 路由登录拦截
$router.beforeEach((to, from, next) => {
	// 要不要对meta.loginRequire属性做监控拦截
	if(to.matched.some(function (item){
		return item.meta.loginRequire
	})){
		let user = $global.session.getUser()
		if($global.tool.isEmpty(user)) next('/login')
		else next()
	}else{
		next();
	}
})

// axios拦截器
$axios.interceptors.request.use(config => {
	let token = $global.session.getUser().token
	if (!$global.tool.isEmpty(token)) {
		config.headers.token = token
	}
	return config
})

$axios.interceptors.response.use(response => {
	return response
}, function(error) {
	if(401 === error.response.status) {
		// window.location = '/login';
		$router.push('/login')
		$global.session.setUser(null)
	}
})
